package com.lsh.payment.service.mispurse;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.lsh.payment.api.model.basevo.BaseResponse;
import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.api.model.mispurse.MisNotifyDto;
import com.lsh.payment.api.service.mispurse.IMisPursePayRestService;
import com.lsh.payment.core.exception.BusinessException;
import com.lsh.payment.core.service.mispurse.impl.PursePayNotifyServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.math.BigDecimal;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 19/3/13
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */

@Service(protocol = "rest", validation = "true")
@Path("pursepay")
//@Consumes({MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8})
@Slf4j
public class MisPursePursePayRestServiceImpl implements IMisPursePayRestService {

    @Autowired
    private PursePayNotifyServiceImpl pursePayNotifyService;

    @POST
    @Path("notify")
    @Override
    public BaseResponse purseNotify(@FormParam("pay_payment_no") String pay_payment_no, @FormParam("trade_id") String trade_id, @FormParam("money") String money) {

        BaseResponse response = new BaseResponse();
        MisNotifyDto misNotifyDto = new MisNotifyDto();
        misNotifyDto.setPay_payment_no(pay_payment_no);
        misNotifyDto.setTrade_id(trade_id);

        try {
            misNotifyDto.setMoney(new BigDecimal(money));

            log.info("notify 参数" + JSON.toJSONString(misNotifyDto));

            pursePayNotifyService.dealNotify(misNotifyDto);

            response.setRet(Integer.parseInt(ExceptionStatus.SUCCESS.getCode()));
            response.setMsg(ExceptionStatus.SUCCESS.getMessage());
        } catch (BusinessException e) {
            log.error("【业务异常】:code is {},message is {}.", new String[]{e.getCode(), e.getMessage()});
            response.setRet(Integer.parseInt(ExceptionStatus.E3001001.getCode()));
            response.setMsg(ExceptionStatus.E3001001.getMessage());
        } catch (Exception e) {
            log.error("【服务端异常】", e);

        }

        return response;
    }
}
